/*
 * Copyright (c) 2022, IMMORTA Inc.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * - Redistributions of source code must retain the above copyright notice, this list
 *   of conditions and the following disclaimer.
 *
 * - Redistributions in binary form must reproduce the above copyright notice, this
 *   list of conditions and the following disclaimer in the documentation and/or
 *   other materials provided with the distribution.
 *
 * - Neither the name of IMMORTA Inc. nor the names of its
 *   contributors may be used to endorse or promote products derived from this
 *   software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef CRC_REG_ACCESS_H
#define CRC_REG_ACCESS_H

/*!
 * @file crc_reg_access.h
 * @brief This file declares or defines crc register access functions
 */

/*******Includes***************************************************************/
#include "device_registers.h"
#include "crc_drv.h"

/*******Definitions************************************************************/
#define CRC_CTRL_TCRC(x)    (((uint32_t)(((uint32_t)(x)) << CRC_CTRL_TCRC_Pos)) & CRC_CTRL_TCRC_Msk)
#define CRC_CTRL_TOTW(x)    (((uint32_t)(((uint32_t)(x)) << CRC_CTRL_TOTW_Pos)) & CRC_CTRL_TOTW_Msk)
#define CRC_CTRL_TOTR(x)    (((uint32_t)(((uint32_t)(x)) << CRC_CTRL_TOTR_Pos)) & CRC_CTRL_TOTR_Msk)
#define CRC_CTRL_XOR(x)     (((uint32_t)(((uint32_t)(x)) << CRC_CTRL_XOR_Pos))  & CRC_CTRL_XOR_Msk)

/*******APIs*******************************************************************/
#if defined(__cplusplus)
extern "C" {
#endif

/*!
 * @brief Sets the CRC protocol width
 *
 * @param[in] regBase:  The CRC peripheral register base address
 * @param[in] width: The CRC protocol width
 * @return None
 */
static inline void CRC_REG_SetProtocol(CRC_Type* regBase,
                                       crc_protocol_t width)
{
    regBase->CTRL = ((regBase->CTRL & ~CRC_CTRL_TCRC_Msk) | CRC_CTRL_TCRC(width));
}

/*!
 * @brief Gets the CRC protocol width
 *
 * @param[in] regBase: The CRC peripheral register base address
 * @return CRC protocol
 */
static inline crc_protocol_t CRC_REG_GetProtocol(const CRC_Type* regBase)
{
    crc_protocol_t retProt = (crc_protocol_t)((regBase->CTRL & CRC_CTRL_TCRC_Msk) >> CRC_CTRL_TCRC_Pos);

    return retProt;
}

/*!
 * @brief Sets the CRC transpose type for writes
 *
 * @param[in] regBase:   The CRC peripheral register base address
 * @param[in] transp: The CRC write transpose type
 * @return None
 */
static inline void CRC_REG_SetWriteTranspose(CRC_Type* regBase,
                                             crc_transpose_t transp)
{
    regBase->CTRL = ((regBase->CTRL & ~CRC_CTRL_TOTW_Msk) | CRC_CTRL_TOTW(transp));
}


/*!
 * @brief Gets the CRC transpose type for reads
 *
 * @param[in] regBase: The CRC peripheral register base address
 * @return CRC write transpose type
 */
static inline crc_transpose_t CRC_REG_GetWriteTranspose(const CRC_Type* regBase)
{
    crc_transpose_t retTransp = (crc_transpose_t)((regBase->CTRL & CRC_CTRL_TOTW_Msk) >> CRC_CTRL_TOTW_Pos);

    return retTransp;
}

/*!
 * @brief Sets the CRC transpose type for reads
 *
 * @param[in] regBase:   The CRC peripheral register base address
 * @param[in] transp: The CRC read transpose type
 * @return None
 */
static inline void CRC_REG_SetReadTranspose(CRC_Type* regBase,
                                            crc_transpose_t transp)
{
    regBase->CTRL = ((regBase->CTRL & ~CRC_CTRL_TOTR_Msk) | CRC_CTRL_TOTR(transp));
}

/*!
 * @brief Gets the CRC transpose type for reads
 *
 * @param[in] regBase: The CRC peripheral register base address
 * @return CRC read transpose type
 */
static inline crc_transpose_t CRC_REG_GetReadTranspose(const CRC_Type* regBase)
{
    crc_transpose_t retTransp = (crc_transpose_t)((regBase->CTRL & CRC_CTRL_TOTR_Msk) >> CRC_CTRL_TOTR_Pos);

    return retTransp;
}

/*!
 * @brief Sets complement read of CRC data register
 *
 * This function sets complement read of CRC data register.
 * Some CRC protocols require the final checksum to be XORed with 0xFFFFFFFF
 * or 0xFFFF. Complement mode enables "on the fly" complementing of read data
 *
 * @param[in] regBase:        The CRC peripheral register base address
 * @param[in] checksumXOR: Enable or disable complementing of read data
 * @return None
 */
static inline void CRC_REG_SetChecksumXor(CRC_Type* regBase,
                                          bool checksumXOR)
{
    regBase->CTRL = ((regBase->CTRL & ~CRC_CTRL_XOR_Msk) | CRC_CTRL_XOR(checksumXOR ? 1U : 0U));
}

/*!
 * @brief Gets complement read of CRC data register
 *        Some CRC protocols require the final checksum to be XORed with 0xFFFFFFFF
 *        or 0xFFFF. Complement mode enables "on the fly" complementing of read data
 *
 * @param[in] regBase: The CRC peripheral register base address
 * @return Complement read
 *         -true: Invert or complement the read value of the CRC Data register
 *         -false: No XOR on reading.
 */
static inline bool CRC_REG_GetChecksumXor(const CRC_Type* regBase)
{
    return ((regBase->CTRL & CRC_CTRL_XOR_Msk) >> CRC_CTRL_XOR_Pos);
}


/*!
 * @brief Sets the polynomial register
 *
 * @param[in] regBase: The CRC peripheral register base address
 * @param[in] poly: Polynomial value
 * @return None
 */
static inline void CRC_REG_SetPoly(CRC_Type* regBase,
                                   uint32_t poly)
{
    regBase->POLYu.POLY = poly;
}

/*!
 * @brief Gets the polynomial register value
 *
 * @param[in] regBase: The CRC peripheral register base address
 * @return Returns the polynomial register value
 */
static inline uint32_t CRC_REG_GetPoly(const CRC_Type* regBase)
{
    return regBase->POLYu.POLY;
}

/*!
 * @brief Sets the 32 bits of CRC data register
 *
 * @param[in] regBase:  The CRC peripheral register base address
 * @param[in] value: New data for CRC computation
 * @return None
 */
static inline void CRC_REG_SetData(CRC_Type* regBase,
                                   uint32_t value)
{
    regBase->DATAu.DATA = value;
}

/*!
 * @brief Sets the lower 16 bits of CRC data register
 *
 * @param[in] regBase:  The CRC peripheral register base address
 * @param[in] value: New data for CRC computation
 * @return None
 */
static inline void CRC_REG_SetDataL(CRC_Type* regBase,
                                    uint16_t value)
{
    regBase->DATAu.DATA_16.LOW = value;
}

/*!
 * @brief Sets the Low Lower Byte - LL
 *
 * @param[in] regBase:  The CRC peripheral register base address
 * @param[in] value: New data for CRC computation
 * @return None
 */
static inline void CRC_REG_SetDataLL(CRC_Type* regBase,
                                     uint8_t value)
{
    regBase->DATAu.DATA_8.LL = value;
}

/*!
 * @brief Gets the current CRC result
 *
 * @param[in] regBase: The CRC peripheral register base address
 * @return Returns the current CRC result
 */
static inline uint32_t CRC_REG_GetData(const CRC_Type* regBase)
{
    /* The dummy read For the result get correctly after optimized */
    (void) regBase->DATAu.DATA;
    return regBase->DATAu.DATA;
}

/*!
 * @brief Gets the current CRC lower 16 bits result
 *
 * @param[in] regBase: The CRC peripheral register base address
 * @return Returns the current CRC result
 */
static inline uint16_t CRC_REG_GetDataL(const CRC_Type* regBase)
{
    return regBase->DATAu.DATA_16.LOW;
}

/*!
 * @brief Sets the 32 bits of CRC data register
 *
 * @param[in] regBase:  The CRC peripheral register base address
 * @param[in] value: New data for CRC computation
 * @return None
 */
static inline void CRC_REG_SetSeed(CRC_Type* regBase,
                                   uint32_t value)
{
    regBase->SEEDu.SEED = value;
}

/*!
 * @brief Sets the lower 16 bits of CRC data register
 *
 * @param[in] regBase:  The CRC peripheral register base address
 * @param[in] value: New data for CRC computation
 * @return None
 */
static inline void CRC_REG_SetSeedL(CRC_Type* regBase,
                                    uint16_t value)
{
    regBase->SEEDu.SEED_16.LOW = value;
}

#if defined(__cplusplus)
}
#endif

#endif /* CRC_REG_ACCESS_H */

/*******EOF********************************************************************/
